package controllers

import (
	"blackgo/app/library/data"
	"blackgo/config/msg"
	"blackgo/utils"
	"net/http"
)

func QueryRows(w http.ResponseWriter, r *http.Request) {
	ResponseInit()

	keyword := ""
	if r.Method == "POST" {
		keyword = r.FormValue("keyword")
	}
	response.Data = data.QueryRows(keyword)

	ResponseJson(w)
}

func QueryEvaluate(w http.ResponseWriter, r *http.Request) {
	ResponseInit()

	if r.Method == "POST" {
		id := r.FormValue("id")
		if id < "1" {
			response = msg.Common["request_parameter_invalid"]
		} else {
			response.Data = data.QueryEvaluate(id)
		}
	} else {
		response = msg.Common["request_method_invalid"]
	}

	ResponseJson(w)
}

func QueryRow(w http.ResponseWriter, r *http.Request) {
	ResponseInit()

	if r.Method == "POST" {
		id := r.FormValue("id")
		if id < "1" {
			response = msg.Common["request_parameter_invalid"]
		} else {
			response.Data = data.QueryRow(id)
		}
	} else {
		response = msg.Common["request_method_invalid"]
	}

	ResponseJson(w)
}

func SaveInfo(w http.ResponseWriter, r *http.Request) {
	ResponseInit()

	if r.Method == "POST" {
		id := r.FormValue("id")
		name := r.FormValue("name")
		intro := r.FormValue("desc")
		types := r.FormValue("type")
		content := r.FormValue("content")
		ip := utils.GetIPAdress(r)
		response = data.SaveInfo(id, name, intro, types, content, ip)
	} else {
		response = msg.Common["request_method_invalid"]
	}

	ResponseJson(w)
}

func SaveEvaluate(w http.ResponseWriter, r *http.Request) {
	ResponseInit()

	if r.Method == "POST" {
		company_id := r.FormValue("company_id")
		content := r.FormValue("content")
		types := r.FormValue("type")
		ip := utils.GetIPAdress(r)
		response = data.SaveEvaluate(company_id, content, types, ip)
	} else {
		response = msg.Common["request_method_invalid"]
	}

	ResponseJson(w)
}

func RemoveInfo(w http.ResponseWriter, r *http.Request) {
	ResponseInit()

	if r.Method == "POST" {
		id := r.FormValue("id")
		if id < "1" {
			response = msg.Common["request_parameter_invalid"]
		} else {
			data.RemoveInfo(id)
		}
	} else {
		response = msg.Common["request_method_invalid"]
	}

	ResponseJson(w)
}
